package com.wgs.onomastici;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.content.res.AssetManager;
import android.util.Log;

public class XmlParser {
	
	
	private static XmlParser instance;
	
	private Document document;
	
	public static XmlParser getInstance(AssetManager manager) throws IOException, SAXException, ParserConfigurationException{
		
		if (instance==null){
			instance=new XmlParser(manager);
			Log.i("test", "new istance");
		}
		
		return instance;
		
	}
	
	
	
	private XmlParser(AssetManager manager) throws IOException, SAXException, ParserConfigurationException {
		super();
		
		InputStream stream = manager.open("santi.xml");
		
		document=parse(stream);
		stream.close();
	}



	public Document getDocument(){
		
		return document;

	}
	
	
	private Document parse(InputStream inputStream) throws SAXException, ParserConfigurationException{
		Document document = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try{
			DocumentBuilder db = factory.newDocumentBuilder();
			InputSource inputSource = new InputSource(inputStream);
			document = db.parse(inputSource);
		}catch(IOException e){e.printStackTrace();}
		return document;
	}
	
	

}
